import React, { Component } from 'react';
import css from './index.less';
import FilterSearch from '@/components/FilterSearch';
import TableList from '@/components/TableList';
import EditModal from '@/components/EditModal';
import { Button, Divider } from 'antd';
import { connect } from 'dva';
import { timeFormat } from '@/utils/common';
import { Link } from 'react-router-dom';
import AuthButton from '@/components/AuthButton';

type State = {
  selectProject: any;
};
type Props = {
  dispatch: Function;
};
class Department extends Component<Props> {
  state: State = {
    selectProject: {},
  };
  table = null;

  filters = [
    {
      key: 'serviceTypeUuid',
      type: 'select',
      label: '选择年份',
      options: this.props.dict.searchYear,
    },
    {
      key: 'budget',
      type: 'select',
      label: '审批投资额',
      options: this.props.dict.budget,
    },
    {
      key: 'approveStatus',
      type: 'select',
      label: '评审状态',
      options: this.props.dict.approveStatus,
    },
    {
      key: 'no',
      type: 'input',
      label: '项目ID',
    },
    {
      key: 'code',
      type: 'input',
      label: '项目代码',
    },
    {
      key: 'number',
      type: 'input',
      label: '审批文号',
    },
    {
      key: 'name',
      type: 'input',
      label: '项目名称',
    },
  ];
  columns = [
    {
      title: '登记日期',
      dataIndex: ['project', 'createTime'],
      width: 150,
      render: (text, data) => timeFormat(data.project.createTime, 'YYYY-MM-DD'),
    },
    {
      title: '项目名称',
      dataIndex: ['project', 'name'],
      render: (text, data) => (
        <Button onClick={() => this.goDetail(data, true)} type="link">
          {text}
        </Button>
      ),
    },
    { title: '立项日期', dataIndex: 'date', width: 180, format: 'YYYY-MM-DD' },
    {
      title: '项目ID',
      dataIndex: 'project.no',
      render: (text, data) => data.project.no,
    },
    {
      title: '项目代码',
      dataIndex: 'project.code',
      render: (text, data) => data.project.code,
    },
    { title: '审批文号', dataIndex: 'number' },
    { title: '审批单位', dataIndex: 'org' },
    { title: '审批投资额（万元）', dataIndex: 'investment' },
    {
      title: '操作',
      width: 325,
      render: (text: string, data: any) => (
        <span key={text}>
          <AuthButton
            onClick={() => this.edit(data, false)}
            role="edit_project_info"
          >
            编辑
          </AuthButton>
          <Divider type="vertical" />
          <AuthButton onClick={() => this.delete(data)} role="del_project_info">
            删除
          </AuthButton>
        </span>
      ),
    },
  ];
  goDetail = ({ uuid }, readOnly) => {
    const url = `projectFillIn/setup/edit?id=${uuid ||
      ''}&type=1&readOnly=${readOnly || ''}`;
    this.props.dispatch({
      type: '$router/forward',
      payload: {
        url,
      },
    });
  };
  edit = (data: any, readOnly: boolean) => {
    const url = `projectFillIn/setup/edit?id=${data.uuid ||
      ''}&readOnly=${readOnly || ''}`;

    this.props.dispatch({
      type: '$router/forward',
      payload: {
        url,
      },
    });
  };

  delete(data) {
    this.props
      .dispatch({
        type: 'base/delete',
        payload: {
          url: '/api/topic/v1/del',
          type: '立项登记',
          data: {
            uuid: data.uuid,
          },
        },
      })
      .then(() => {
        this.table.fetch();
      });
  }
  render() {
    const { selectProject } = this.state;
    return (
      <div>
        <FilterSearch
          filters={this.filters}
          onSearch={val => this.table.fetch(val)}
        >
          <AuthButton type="primary" role="see_project_info">
            <Link to={{ pathname: '/projectFillIn/setup/edit' }}>添加</Link>
          </AuthButton>
        </FilterSearch>
        <TableList
          post
          rowKey="id"
          onRef={c => {
            this.table = c;
          }}
          columns={this.columns}
          source="/api/topic/v1/query"
        />
      </div>
    );
  }
}

export default connect(state => state.base)(Department);
